package com.h2View.utils;

import com.h2View.exception.ServiceException;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.setting.dialect.Props;

import java.io.File;
import java.io.IOException;

/**
 * @author xwx1052336
 * @since 2021/9/15
 */
public class PropUtil {

    private static Log LOG = LogFactory.get();

    private static final File CONFIG = new File("config/init.properties");

    static {
        if(!CONFIG.exists()){
            if(!CONFIG.getParentFile().exists()){
                if(!CONFIG.getParentFile().mkdirs()){
                    throw new ServiceException("创建配置文件父目录失败");
                }
            }
            try{
                if(CONFIG.createNewFile()){
                    LOG.info("配置文件初始化成功");
                }
            }catch (IOException e){
                LOG.error(e);
            }
        }
    }

    private static Props props = new Props(CONFIG);

    public static String getStr(String key){
        return props.getStr(key);
    }

    public static void setProperty(String key, Object value) {
        if(value != null){
            props.setProperty(key,value);
        }
    }

    public static void store(String path){
        props.store(path);
    }

    public static void store(){
        props.store(CONFIG.getAbsolutePath());
    }

    public static String getPath(){
        return CONFIG.getAbsolutePath();
    }

}
